package GUILayer;
import ControlLayer.CtrAttribute;
import ModelLayer.Attribute;
import javax.swing.table.DefaultTableModel;
import java.util.ArrayList;

public class GUIAttribute extends javax.swing.JFrame {
    private CtrAttribute ctrAttribute;

    public GUIAttribute() {
        initComponents();
        ctrAttribute = CtrAttribute.getInstance();
        loadTableContent();
    }

    private void loadTableContent() {
        ((DefaultTableModel)tblAttributeList.getModel()).setRowCount(0);
        ArrayList<Attribute> attributeList = ctrAttribute.getAttributeList();
        for (Attribute attribute : attributeList) {
            String productAttributeIDs = new String();
            for (Integer productAttributeID : attribute.getProductAttributeIDs()) {
                productAttributeIDs += productAttributeID + ", ";
            }
            ((DefaultTableModel)tblAttributeList.getModel()).addRow(
                new Object [] {
                    attribute.getIDAttribute(),
                    attribute.getIDAttributeGroup(),
                    attribute.getColor(),
                    attribute.getName(),
                    productAttributeIDs
                }
            );
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scrlAttributeList = new javax.swing.JScrollPane();
        tblAttributeList = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tblAttributeList.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "id", "id_attribute_group", "color", "name", "productattribute IDs"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        scrlAttributeList.setViewportView(tblAttributeList);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrlAttributeList, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrlAttributeList, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUIAttribute().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane scrlAttributeList;
    private javax.swing.JTable tblAttributeList;
    // End of variables declaration//GEN-END:variables

}
